/*	This file is part of LSAC.

	LSAC is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	LSAC is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with LSAC.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (c) 2010 LEPESME "Jiboo" Jean-Baptiste
*/

#include "cInstAffect.h"
#include "lsac.h"

#include <typeinfo>

cInstAffect::cInstAffect(cLocation pLocation, cBlock *pBlock, string pLValue, cExpression *pRValue)
	: cInstruction(pLocation, pBlock, pLValue)
{
	this->dRValue = pRValue;
}

cInstAffect::~cInstAffect()
{
	delete this->dRValue;
}

void cInstAffect::print(int pLevel)
{
	PRINT_LOG("cInstAffect " << this);
	PRINT_LOG("{");
	PRINT_LOG("LValue: " << this->dLValue);
	PRINT_LOG("RValue: ");
	this->dRValue->print(pLevel + 1);
	PRINT_LOG("}");
}

void cInstAffect::validate(cSemContext *pContext)
{
	if(pContext->varExists(this->dLValue) == NULL)
		ERROR("Erreur en " << this->dLocation << " symbole '" << this->dLValue << "' non déclaré.");

	eType lLType = pContext->varExists(this->dLValue)->dType;
	eType lRType = this->dRValue->getType(pContext);

	if(lLType != lRType)
		ERROR("Erreur en " << this->dLocation << " type de la LValue (" << lLType << ") different de celui de la RValue (" << lRType << ").");
}

void cInstAffect::genCode(ostream &pStream)
{
	pStream << this->dLValue << " = ";
	this->dRValue->genCode(pStream);
	pStream << ";" << endl;
}
